var core    = require("core")
,   crypto  = require('crypto')
,   model   = require('../model')
,   utils   = core.utils
,   send    = core.send
,   authTokens = function(req, next){
    
    var status      = false
    ,   app         = global.app
    ,   token       = req.tokens
    ,   platformId  = token.platform || false
    ,   key         = token.key
    ,   platform    = global.app.platform.item[platformId];
    status = platformId && function(){
        var key = crypto.createHash('sha1');
        key.update(platform.key + platform.time);
        return key.digest('hex');
    }() === key;
    if(false === status){
        var SEND    = new send(900)
        ,   error   = new Error(SEND.msg);
        error.status = 900;
        return next(error);
    };
    return next();

};

module.exports = authTokens;
